package com.mvp.utils;

import android.app.Activity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import static android.content.Context.INPUT_METHOD_SERVICE;

public class InputMethodUtils {

    public static InputMethodManager getInputMethodManager(Activity context) {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE);
        return inputMethodManager;
    }

    /**
     * 打开输入法
     */
    public static void showSoftInput(Activity context) {
        showSoftInput(context, null);
    }

    /**
     * 打开输入法
     */
    public static void showSoftInput(Activity context, View view) {
        InputMethodManager imm = getInputMethodManager(context);
        if (view == null) {
            imm.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } else {
            view.requestFocus();
            imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
        }
    }

    public static void closeKeyBoard(Activity context) {
        View view = context.getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    /*	((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(InfoSrchRsltActivity.this.getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);*/
    }

    public static void hideSoftInputFromWindow(Activity context, View view) {
        view.clearFocus();
        InputMethodManager imm = getInputMethodManager(context);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
    }

//	public void showInputMethod(View view) {
//		inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
//	}
//
//	/**
//	 * 隐藏输入法
//	 *
//	 */
//	public void dismissInputMethod() {
//		inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//	}
//
//	public void showAndHideInputMethpd() {
//		inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//	}
//
//	public boolean isInputOpen() {
//		return inputMethodManager.isActive();
//	}
//


}
